package com.gilgamesh.mall.product.service.impl;

import com.gilgamesh.mall.product.dao.ClassficDao;
import com.gilgamesh.mall.product.service.ClassficService;
import com.gilgamesh.mall.sdk.bean.product.ClassficEntity;
import com.gilgamesh.mall.sdk.dto.ResponseDTO;
import com.gilgamesh.mall.sdk.dto.product.ClassficDTO;
import com.gilgamesh.mall.sdk.util.BeanUtil;
import com.gilgamesh.mall.sdk.util.CommonException;
import com.gilgamesh.mall.sdk.util.ResponseUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yanm
 * @date 2021/1/11 14:09
 **/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClassficServiceImpl implements ClassficService {

    private final ClassficDao classficDao;

    @Override
    public ResponseDTO<Integer> addClassfic(ClassficEntity classficEntity) {
        if (log.isDebugEnabled()) {
            log.debug("classEntity is,{}", classficEntity);
        }
        if (BeanUtil.isLogicalNull(classficEntity)) {
            throw new CommonException("classficEntity不能为空");
        }
        classficEntity.setCreateTime(new Date());
        return ResponseUtil.getResponse(classficDao.add(classficEntity));
    }

    /**
     * 高级查询
     *
     * @param classficEntity 分类实例
     * @return 响应
     * @author yanm
     * @date 2021/1/11 16:30
     **/
    @Override
    public ResponseDTO<List<ClassficDTO>> listClassfics(ClassficEntity classficEntity) {
        if (log.isDebugEnabled()) {
            log.debug("classEntity is,{}", classficEntity);
        }

        return ResponseUtil.getResponse(classficDao.listClassfics(classficEntity));
    }

    @Override
    public ResponseDTO<ClassficDTO> getClassficById(Long id){
        ResponseDTO<ClassficDTO> responseDTO = new ResponseDTO<>();
        ClassficDTO classficDTO = classficDao.getById(id);
        responseDTO.setData(classficDTO);
        if(BeanUtil.isLogicalNull(responseDTO)){
            responseDTO.setCode("0");
            responseDTO.setMessage("success");
            responseDTO.setSuccess(Boolean.TRUE);
        }
        return responseDTO;
    }

    @Override
    public ResponseDTO<ClassficDTO> listTreeById(Long id) {
        ClassficDTO root = this.getClassficById(id).getData();
        List<ClassficDTO> all = this.listClassfics(null).getData();
        List<ClassficDTO> collect = all.stream().filter(e->e.getSuperiorId().equals(root.getId()
        )).peek(e-> e.setChildern(getClildern(e,all))).collect(Collectors.toList());
        root.setChildern(collect);
        return ResponseUtil.getResponse(root);
    }

    @Override
    public ResponseDTO<List<ClassficDTO>> listTree() {
        ClassficEntity entity = new ClassficEntity();
        entity.setLevel(Byte.parseByte("1"));
        List<ClassficDTO> roots =  this.listClassfics(entity).getData();
        for(ClassficDTO root:roots) {
            List<ClassficDTO> all = this.listClassfics(null).getData();
            List<ClassficDTO> collect = all.stream().filter(e -> e.getSuperiorId().equals(root.getId())).map(e -> {
                e.setChildern(getClildern(e, all));
                return e;
            }).collect(Collectors.toList());
            root.setChildern(collect);
        }
        return ResponseUtil.getResponse(roots);
    }

    private List<ClassficDTO> getClildern(ClassficDTO root,List<ClassficDTO> all){
        return all.stream().filter(e-> Objects.equals(e.getSuperiorId(),root.getId())).map(e->{
            e.setChildern(getClildern(e,all));
            return e;
        }).collect(Collectors.toList());
    }




}
